package javango.forms.fields;

import java.util.Map;

import javango.forms.widgets.WidgetFactory;

import org.apache.commons.lang.StringUtils;

import com.google.inject.Inject;

/**
 * Implements a field that cleans itself as an Double value.
 * 
 * @author johns
 *
 */
public class DoubleField extends FieldBase {

	@Inject
	public DoubleField(WidgetFactory widgetFactory) {
		super(widgetFactory);
	}

	public Object clean(String value, Map<String, String> errors) {
		if (StringUtils.isEmpty(value)) {
			if (this.isRequired()) {
				errors.put(getName(), "Required");
				return null;
			} else {
				return null;
			}
		}
		try {
			return Double.valueOf(value);
		} catch (NumberFormatException e) {
			errors.put(this.getName(), "Double number not valid");
		}
		return null;
	}

}
